home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / imgread.c < prev    next >
C/C++ Source or Header  |  1990-04-26  |  6KB  |  235 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    imgread.c (Image Read)
  6.  * Purpose:    Read image headers and image files
  7.  * Subroutine:    init_image()            returns: int
  8.  * Subroutine:    load_image()
  9.  * Xlib calls:    none
  10.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Modified:    {0} Michael VanHilst    initial version             1 July 1989
  17.  *        {n} <who> -- <does what> -- <when>
  18.  */
  19.  
  20. #include <stdio.h>            /* stderr, NULL, etc. */
  21. #include <X11/Xlib.h>            /* X window stuff */
  22. #include <X11/Xutil.h>            /* X window manager stuff */
  23. #include "hfiles/constant.h"        /* constants and codes */
  24. #include "hfiles/struct.h"        /* declare structure types */
  25. #include "hfiles/extern.h"        /* extern main parameter structures */
  26.  
  27. static int init_imtool;
  28.  
  29. /*
  30.  * Subroutine:    init_image
  31.  * Purpose:    Set the image dimensions
  32.  * Returns:    1 if OK, 0 if trouble
  33.  * Method:    set image info from known values or read from a header
  34.  */
  35. int init_image ( )
  36. {
  37.   int get_fbconfig();        /* get IRAF expected frame size */
  38.   void init_img(), init_dispcen();
  39. #ifdef FITS
  40.   int init_fits();
  41. #endif
  42. #ifdef OIF
  43.   int init_irafimh();
  44. #endif
  45.  
  46.   /* read and strip subsection suffix */
  47. /* %%  get_subsection(&img); */
  48.   switch( img.file_type ) {
  49.   case SOP_Array:
  50. #ifdef DEBUG
  51.     if( (img.filerows <= 0) || (img.filecols <= 0) ) {
  52.       (void)fprintf(stderr, "Error: array dimensions not set");
  53.       return( 0 );
  54.     }
  55. #endif
  56.     img.block_type = SOP_ZoomNone;
  57.     break;
  58.   case SOP_FITS:
  59. #ifdef FITS
  60.     if( (img.fd = init_fits(&img)) < 0 )
  61.       return( 0 );
  62.     img.block_type = SOP_ZoomNone;
  63.     break;
  64. #else
  65.     (void)fprintf(stderr, "WARNING: not compiled with -DFITS switch\n");
  66.     return( 0 );
  67. #endif
  68.   case SOP_IRAF:
  69. #ifdef OIF
  70.     if( (img.fd = init_irafimh(&img)) < 0 )
  71.       return( 0 );
  72.     img.block_type = SOP_ZoomNone;
  73.     break;
  74. #else
  75.     (void)fprintf(stderr, "WARNING: not compiled with -DIRAF switch\n");
  76.     return( 0 );
  77. #endif
  78.   case SOP_SAOCCD:
  79. #ifdef SAOCCD
  80.     init_saoccd(&img);
  81.     img.block_type = SOP_ZoomNone;
  82.     break;
  83. #else
  84.     (void)fprintf(stderr, "WARNING: not compiled with -DSAOCCD switch\n");
  85.     return( 0 );
  86. #endif
  87.   case SOP_Imtool:
  88.   case SOP_PROS:
  89. #ifdef IMTOOL
  90.     /* remote access from iraf, start with standard screen size */
  91.     if( get_fbconfig(img.fbconfig, &img.filecols, &img.filerows)
  92.         == 0 ) {
  93.       img.filecols = 512;
  94.       img.filerows = 512;
  95.     }
  96.     img.block_type = SOP_ZoomNone;
  97.     /* signal load_image to load a default image */
  98.     init_imtool = 1;
  99.     break;
  100. #else
  101.     (void)fprintf(stderr, "Note: IRAF pipe support not compiled.\n");
  102.     return( 0 );
  103. #endif
  104.   case SOP_Logo:
  105.   default:
  106.     img.filerows = 512;
  107.     img.filecols = 512;
  108.     img.block_type = SOP_ZoomNone;
  109.     break;
  110.   }
  111.   /* fix up image record parameters and img to file conversions */
  112.   init_img(&img, &coord);
  113.   init_dispcen(&img, &coord);
  114.   return( 1 );
  115. }
  116.  
  117. /*
  118.  * Subroutine:    load_image
  119.  * Purpose:    Open if necessary, then read the appropriate image file
  120.  *        and create a 16 bit image array
  121.  */
  122. void load_image ( fb, buf, bftrans, imgbuf, filebuf, buf_squared, buf_doubled )
  123.      Edges *fb;            /* coord structures (for disp or pan) */
  124.      Coordsys *buf;
  125.      Transform *bftrans;
  126.      short *imgbuf;
  127.      char *filebuf;
  128.      int buf_squared, buf_doubled;    /* buf is larger to enable rotation */
  129. {
  130.   static int init = 1;    /* signal first time through */
  131.   int x, y;
  132.   int width, height;
  133.   int read_image;    /* avoid doing rotate if there is nothing to rotate */
  134.   void read_array(), rotate_buf(), clear_coord_area(), load_logo();
  135.  
  136. #ifdef SUBSEC
  137.   float X, Y;
  138.   void i_transform();
  139.   /* THIS ISN'T RIGHT (WHAT IF IMAGE IS ROTATED OR FLIPPED?) */
  140.   /* get file coords of buffer's 0,0 element */
  141.   i_transform(bftrans, 0, 0, &X, &Y);
  142.   x = (int)(X + 0.5);
  143.   y = (int)(Y + 0.5);
  144. #else
  145.   x = 0;
  146.   y = 0;
  147.   fb->block = 1;
  148. #endif
  149.   /* if rotation has a 90 degree component, get cross zoom factor */
  150.   if( img.rotate_code & 1 ) {
  151.     width = buf->height;
  152.     height = buf->width;
  153.   } else {
  154.     width = buf->width;
  155.     height = buf->height;
  156.   }
  157.   read_image = 0;
  158.   switch(img.file_type) {
  159.   case SOP_Array:
  160.     read_array(-1, &img, imgbuf, filebuf, width, height, x, y,
  161.            fb->block, control.verbose);
  162.     read_image = 1;
  163.     break;
  164. #ifdef FITS
  165.   case SOP_FITS:
  166. #endif
  167. #ifdef OIF
  168.   case SOP_IRAF:
  169. #endif
  170.     read_array(img.fd, &img, imgbuf, filebuf,
  171.            width, height, x, y, fb->block, control.verbose);
  172.     read_image = 1;
  173.     break;
  174. #ifdef SAOCCD
  175.   case SOP_SAOCCD:
  176.     load_saoccd(&img, imgbuf, width, height, x, y, fb->block);
  177.     read_image = 1;
  178.     break;
  179. #endif
  180. #ifdef XRAY
  181.   case SOP_HRI:
  182.   case SOP_IPC:
  183.     load_einstein(&img, imgbuf, width, height,
  184.           fb->cenX, fb->cenY, fb->block);
  185.     read_image = 1;
  186.     break;
  187. #endif
  188. #ifdef ROSAT
  189.   case SOP_ROSAT:
  190.     load_rosat(&img, imgbuf, width, height, fb->cenX, fb->cenY, fb->block);
  191.     read_image = 1;
  192.     break;
  193. #endif
  194. #ifdef IMTOOL
  195.   case SOP_Imtool:
  196.   case SOP_PROS:
  197.     if( init )
  198.       load_logo(imgbuf, width, height);
  199.     if( init_imtool ) {
  200.       init_imtool = 0;
  201.     }
  202.     break;
  203. #endif
  204.   case SOP_Logo:
  205.     if( init )
  206.       load_logo(imgbuf, width, height);
  207.   default:
  208.     break;
  209.   }
  210.   if( read_image ) {
  211.     if( (img.row_order != 0) || (img.rotate_code != 0) ) {
  212.       rotate_buf(imgbuf, img.row_order, img.rotate_code, width, height,
  213.          buf_squared, buf_doubled);
  214.     }
  215.     if( (img.file_type != SOP_Imtool) && (img.file_type != SOP_PROS) ) {
  216.       /* we loaded a non-imtool image */
  217.       img.imtool_200 = 0;
  218.       /* if there was a second coordinate system, cancel it */
  219.       if( coord.imtool_aux ) {
  220.     clear_coord_area();
  221.     coord.imtool_aux = 0;
  222.       }
  223.       /* if artificial limits were applied to input, mark short_buf extremes */
  224.       if( img.fimin < img.fimax ) {
  225.     buffer.clipmin = (int)((img.fimin - img.fibias) / img.fiscale);
  226.     buffer.clipmax = (int)((img.fimax - img.fibias) / img.fiscale);
  227.       } else {
  228.     /* imtool limits are set when an image is read from the pipe */
  229.     buffer.clipmin = -32768;
  230.     buffer.clipmax = 32767;
  231.       }
  232.     }
  233.   }
  234. }
  235.